﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace HouseOfCards
{
   /// <summary>
   /// Provides card images based on a standard 52-card deck
   /// </summary>
   public partial class CardImage : UserControl
   {

      private byte _cardValue;
      private bool _isHidden;

      /// <summary>
      /// Value of the card (0-53)
      /// </summary>
      [Category("Appearance"), Description("Gets or sets the value of the card"), DefaultValue(53)]
      public byte CardValue
      {
         get { return _cardValue; }
         set { 
            _cardValue = value;
            SelectImage(_cardValue);
         }
      }

     //public Point OldPoint;

      /// <summary>
      /// Creates a card image
      /// </summary>
      public CardImage()
      {
         InitializeComponent();
         this.CardValue = (byte)CardValues.CardBack;
         _isHidden = true;
         SelectImage(this.CardValue);
         this.Refresh();
      }

      /// <summary>
      /// Sets the value of the card image
      /// </summary>
      /// <param name="cardValue">byte value of the card</param>
      public void SetCardValue(byte cardValue)
      {
         this.CardValue = cardValue;
         
         SelectImage(this.CardValue);

         if (cardValue == (byte)CardValues.CardBack)
         {
            _isHidden = true;
         }
         else
         {
            _isHidden = false;
         }

         this.Refresh();
      }

      /// <summary>
      /// Sets the value of the card image
      /// </summary>
      /// <param name="CardImageValue"><see cref="CardValues"/></param>
      public void SetCardValue(CardValues CardImageValue)
      {
         byte cardValue = (byte)CardImageValue;
         SetCardValue(cardValue);
      }

      /// <summary>
      /// Hides the card by flipping it over to show back of card
      /// </summary>
      public void HideCard()
      {
         SelectImage(CardValues.CardBack);
         _isHidden = true;
         this.Refresh();
      }

      /// <summary>
      /// Flips the card over
      /// </summary>
      public void FlipCard()
      {
         if (_isHidden)
         {
            SelectImage(this._cardValue);
            _isHidden = false;
         }
         else
         {
            SelectImage(CardValues.CardBack);
            _isHidden = true;
         }
      }

      private void SelectImage(CardValues cardImageValue)
      {
         byte cardValue = (byte)cardImageValue;
         SetCardValue(cardValue);
      }

      private void SelectImage(byte cardValue)
      {
         switch (cardValue)
         {
            case 0:
               this.pbImage.Image = global::HouseOfCards.Resource1.c1;
               break;
            case 1:
               this.pbImage.Image = global::HouseOfCards.Resource1.d1;
               break;
            case 2:
               this.pbImage.Image = global::HouseOfCards.Resource1.h1;
               break;
            case 3:
               this.pbImage.Image = global::HouseOfCards.Resource1.s1;
               break;
            case 4:
               this.pbImage.Image = global::HouseOfCards.Resource1.c2;
               break;
            case 5:
               this.pbImage.Image = global::HouseOfCards.Resource1.d2;
               break;
            case 6:
               this.pbImage.Image = global::HouseOfCards.Resource1.h2;
               break;
            case 7:
               this.pbImage.Image = global::HouseOfCards.Resource1.s2;
               break;
            case 8:
               this.pbImage.Image = global::HouseOfCards.Resource1.c3;
               break;
            case 9:
               this.pbImage.Image = global::HouseOfCards.Resource1.d3;
               break;
            case 10:
               this.pbImage.Image = global::HouseOfCards.Resource1.h3;
               break;
            case 11:
               this.pbImage.Image = global::HouseOfCards.Resource1.s3;
               break;
            case 12:
               this.pbImage.Image = global::HouseOfCards.Resource1.c4;
               break;
            case 13:
               this.pbImage.Image = global::HouseOfCards.Resource1.d4;
               break;
            case 14:
               this.pbImage.Image = global::HouseOfCards.Resource1.h4;
               break;
            case 15:
               this.pbImage.Image = global::HouseOfCards.Resource1.s4;
               break;
            case 16:
               this.pbImage.Image = global::HouseOfCards.Resource1.c5;
               break;
            case 17:
               this.pbImage.Image = global::HouseOfCards.Resource1.d5;
               break;
            case 18:
               this.pbImage.Image = global::HouseOfCards.Resource1.h5;
               break;
            case 19:
               this.pbImage.Image = global::HouseOfCards.Resource1.s5;
               break;
            case 20:
               this.pbImage.Image = global::HouseOfCards.Resource1.c6;
               break;
            case 21:
               this.pbImage.Image = global::HouseOfCards.Resource1.d6;
               break;
            case 22:
               this.pbImage.Image = global::HouseOfCards.Resource1.h6;
               break;
            case 23:
               this.pbImage.Image = global::HouseOfCards.Resource1.s6;
               break;
            case 24:
               this.pbImage.Image = global::HouseOfCards.Resource1.c7;
               break;
            case 25:
               this.pbImage.Image = global::HouseOfCards.Resource1.d7;
               break;
            case 26:
               this.pbImage.Image = global::HouseOfCards.Resource1.h7;
               break;
            case 27:
               this.pbImage.Image = global::HouseOfCards.Resource1.s7;
               break;
            case 28:
               this.pbImage.Image = global::HouseOfCards.Resource1.c8;
               break;
            case 29:
               this.pbImage.Image = global::HouseOfCards.Resource1.d8;
               break;
            case 30:
               this.pbImage.Image = global::HouseOfCards.Resource1.h8;
               break;
            case 31:
               this.pbImage.Image = global::HouseOfCards.Resource1.s8;
               break;
            case 32:
               this.pbImage.Image = global::HouseOfCards.Resource1.c9;
               break;
            case 33:
               this.pbImage.Image = global::HouseOfCards.Resource1.d9;
               break;
            case 34:
               this.pbImage.Image = global::HouseOfCards.Resource1.h9;
               break;
            case 35:
               this.pbImage.Image = global::HouseOfCards.Resource1.s9;
               break;
            case 36:
               this.pbImage.Image = global::HouseOfCards.Resource1.c10;
               break;
            case 37:
               this.pbImage.Image = global::HouseOfCards.Resource1.d10;
               break;
            case 38:
               this.pbImage.Image = global::HouseOfCards.Resource1.h10;
               break;
            case 39:
               this.pbImage.Image = global::HouseOfCards.Resource1.s10;
               break;
            case 40:
               this.pbImage.Image = global::HouseOfCards.Resource1.cj;
               break;
            case 41:
               this.pbImage.Image = global::HouseOfCards.Resource1.dj;
               break;
            case 42:
               this.pbImage.Image = global::HouseOfCards.Resource1.hj;
               break;
            case 43:
               this.pbImage.Image = global::HouseOfCards.Resource1.sj;
               break;
            case 44:
               this.pbImage.Image = global::HouseOfCards.Resource1.cq;
               break;
            case 45:
               this.pbImage.Image = global::HouseOfCards.Resource1.dq;
               break;
            case 46:
               this.pbImage.Image = global::HouseOfCards.Resource1.hq;
               break;
            case 47:
               this.pbImage.Image = global::HouseOfCards.Resource1.sq;
               break;
            case 48:
               this.pbImage.Image = global::HouseOfCards.Resource1.ck;
               break;
            case 49:
               this.pbImage.Image = global::HouseOfCards.Resource1.dk;
               break;
            case 50:
               this.pbImage.Image = global::HouseOfCards.Resource1.hk;
               break;
            case 51:
               this.pbImage.Image = global::HouseOfCards.Resource1.sk;
               break;
            case 52:
               this.pbImage.Image = global::HouseOfCards.Resource1.jb;
               break;
            case 53:
               this.pbImage.Image = global::HouseOfCards.Resource1.b1fv;
               break;
         }
      }

      /// <summary>
      /// Method for returning face value based on raw card value.
      /// The formula for card value is (card = suit + face * 4)
      /// </summary>
      /// <param name="CardValue"></param>
      /// <param name="Suit"></param>
      /// <returns></returns>
      private FaceTypes CardFaceValue(byte CardValue, SuitTypes Suit)
      {
         return (FaceTypes)((CardValue - (int)Suit) / 4);
      }

      /// <summary>
      /// Returns the face value of a card
      /// </summary>
      /// <returns><see cref="FaceTypes"/></returns>
      public FaceTypes GetFaceValue()
      {
         return (FaceTypes)(CardFaceValue(this.CardValue, GetSuitValue()));
      }

      /// <summary>
      /// Returns the Suit value of a card
      /// </summary>
      /// <returns><see cref="SuitTypes"/></returns>
      public SuitTypes GetSuitValue()
      {
         switch (this.CardValue)
         {
            case 0:
            case 4:
            case 8:
            case 12:
            case 16:
            case 20:
            case 24:
            case 28:
            case 32:
            case 36:
            case 40:
            case 44:
            case 48:
               return SuitTypes.Clubs;
            //break;
            case 1:
            case 5:
            case 9:
            case 13:
            case 17:
            case 21:
            case 25:
            case 29:
            case 33:
            case 37:
            case 41:
            case 45:
            case 49:
               return SuitTypes.Diamonds;
            //break;
            case 2:
            case 6:
            case 10:
            case 14:
            case 18:
            case 22:
            case 26:
            case 30:
            case 34:
            case 38:
            case 42:
            case 46:
            case 50:
               return SuitTypes.Hearts;
            //break;
            default:
               return SuitTypes.Spades;
            //break;
         }
      }

      private void pbImage_Click(object sender, EventArgs e)
      {
         OnClick(e);
      }

      private void pbImage_DoubleClick(object sender, EventArgs e)
      {
         OnDoubleClick(e);
      }

      private void pbImage_MouseCaptureChanged(object sender, EventArgs e)
      {
         OnMouseCaptureChanged(e);
      }

      private void pbImage_MouseClick(object sender, MouseEventArgs e)
      {
         OnMouseClick(e);
      }

      private void pbImage_MouseDoubleClick(object sender, MouseEventArgs e)
      {
         OnMouseDoubleClick(e);
      }

      /// <summary>
      /// Default size of the card image is 71X96
      /// </summary>
      protected override Size DefaultSize
      {
         get
         {
            return new Size(71, 96);
         }
      }

      #region Overridden properties
      /// <summary>
      /// Excluded
      /// </summary>
      /// <tocexclude/>
      [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
      public override Image BackgroundImage
      {
         get
         {
            return base.BackgroundImage;
         }
         set
         {
            base.BackgroundImage = value;
         }
      }

      /// <summary>
      /// Excluded
      /// </summary>
      /// <tocexclude/>
      [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
      public override ImageLayout BackgroundImageLayout
      {
         get
         {
            return base.BackgroundImageLayout;
         }
         set
         {
            base.BackgroundImageLayout = value;
         }
      }


      /// <summary>
      /// Excluded
      /// </summary>
      /// <tocexclude/>
      [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
      public override Font Font
      {
         get
         {
            return base.Font;
         }
         set
         {
            base.Font = value;
         }
      }

      /// <summary>
      /// Excluded
      /// </summary>
      /// <tocexclude/>
      [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
      public override Color ForeColor
      {
         get
         {
            return base.ForeColor;
         }
         set
         {
            base.ForeColor = value;
         }
      }

      /// <summary>
      /// Excluded
      /// </summary>
      /// <tocexclude/>
      [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
      public override Color BackColor
      {
         get
         {
            return base.BackColor;
         }
         set
         {
            base.BackColor = value;
         }
      }
      #endregion

   }
}
